<?php
namespace ESTool;
require __DIR__ . '/autoload.php';
require __DIR__ . '/../helper.php';
date_default_timezone_set("Etc/GMT-8");
/**
 * 测试类
 * 
 * @author daixianfei
 */
class demo {
    /**
     * 获取ES数据，类似 DSL中的_search请求
     */
    public function getSource() {
        $where = array(
            'cdate'=>20180722,
            'status,neq' => 0,
            "reserve_end_time,lte" => date("Y-m-d"),
            "create_time,gte" => "2016-08-13",
        );
        $result = Elasticsearch::getInstanceNew()
        ->setIndexMapping("es_index_php")
        ->setWhere($where)
        ->setSource(array("_score","tags","soft_name","category_name","single_word","gameid","create_time"))
        ->setSize(5)
        ->setFrom(0)
        ->query()
        ->fetchTableSource();
        return $result;
    }
    /**
     * 脚本评分方法
     */
    public function getScript() {
        $where = array(
            'cdate'=>20180722,
            'status,neq' => 0,
            "reserve_end_time,lte" => date("Y-m-d"),
            "create_time,gte" => "2016-08-13",
        );
        $sortFields = array(
            'total','day1','day7','day3','fans','fans1','fans30','fans7'
        );
        $sortWight = array();
        foreach ($sortFields as $key) {
            $sortWight[$key] = rand(1,10);
        }
        $result = Elasticsearch::getInstanceNew()
        ->setIndexMapping("es_index_php")
        ->setWhere($where)
        ->setScoreFieldsWight($sortWight)
        ->setSource(array("_score","tags","soft_name","category_name","single_word","gameid","create_time"))
        ->setSize(5)
        ->setFrom(10)
        ->query()
        ->fetchTableSource();
        return $result;
    }
    /**
     * 获取聚合查询结果
     * @desc es聚合方法
     * @copyright 0.1
     */
    public function getAggs() {
        $where = array(
            'cdate'=>20180722,
            'status,neq' => 0,
            "reserve_end_time,lte" => date("Y-m-d"),
            "create_time" => array("2018-07-01", "2018-07-10"),
            "category_name,in" => array("休闲益智","体育","塔防"),
        );
        $result = Elasticsearch::getInstanceNew()
        ->setParam(
            $where,
            array("gameid"),
            array("category_name"=>array("size"=>10)),
            array("create_time"=>array("show_field"=>"create_time", "size"=>5))
            )
            ->setIndexMapping("es_index_php")
            ->query()
            ->fetchTable();
            return $result;
    }
}






// ===================
$demo = new demo();
$ref = new \ReflectionClass($demo);
$funcList = $ref->getMethods();
foreach ($funcList as $item) {
    $func = $item->name;
    $res = $demo->{$item->getName()}();
    $data[$item->getName()] = array(
        "title" => $item->getDocComment(),
        "func" => $item->getName(),
        "res" => array_slice($res, 0, 5),
    );
}

p($data);


